home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’97 / StyledEdit / source code / ShowInitIcon.c next >
Encoding:
Text File  |  1997-06-27  |  6.0 KB  |  163 lines  |  [TEXT/CWIE]

  1. // ShowInitIcon - version 1.0.1, May 30th, 1995
  2. // This code is intended to let INIT writers easily display an icon at startup time.
  3. // View in Geneva 9pt, 4-space tabs
  4.  
  5. // Written by: Peter N Lewis <peter@mail.peter.com.au>, Jim Walker <JWWalker@aol.com>
  6. // and François Pottier <pottier@dmi.ens.fr>, with thanks to previous ShowINIT authors.
  7. // Send comments and bug reports to François Pottier.
  8.  
  9. // This version features:
  10. // - Short and readable code.
  11. // - Correctly wraps around when more than one row of icons has been displayed.
  12. // - works with System 6
  13. // - Built with Universal Headers & CodeWarrior. Should work with other headers/compilers.
  14. #define SystemSixOrLater 1
  15.  
  16. #include <Memory.h>
  17. #include <Resources.h>
  18. #include <Icons.h>
  19. #include <OSUtils.h>
  20. #include "ShowInitIcon.h"
  21.  
  22. // You should set SystemSixOrLater in your headers to avoid including glue for SysEnvirons.
  23.  
  24.  
  25. // ---------------------------------------------------------------------------------------------------------------------
  26. // Set this flag to 1 if you want to compile this file into a stand-alone resource (see note below).
  27. // Set it to 0 if you want to include this source file into your INIT project.
  28.  
  29. #if 0
  30. #define ShowInitIcon main
  31. #endif
  32.  
  33. // ---------------------------------------------------------------------------------------------------------------------
  34. // The ShowINIT mechanism works by having each INIT read/write data from these globals.
  35. // The MPW C compiler doesn't accept variables declared at an absolute address, so I use these macros instead.
  36. // Only one macro is defined per variable; there is no need to define a Set and a Get accessor like in <LowMem.h>.
  37.  
  38. #define    LMVCoord            (* (short*) 0x92A)
  39. #define    LMVCheckSum        (* (short*) 0x928)
  40. #define    LMHCoord            (* (short*) 0x92C)
  41. #define    LMHCheckSum        (* (short*) 0x92E)
  42.  
  43. // ---------------------------------------------------------------------------------------------------------------------
  44. // Prototypes for the subroutines. The main routine comes first; this is necessary to make THINK C's "Custom Header" option work.
  45.  
  46. static unsigned short CheckSum (unsigned short x);
  47. static void ComputeIconRect (Rect* iconRect, Rect* screenBounds);
  48. static void AdvanceIconPosition (Rect* iconRect);
  49. static void DrawBWIcon (short iconID, Rect *iconRect);
  50.  
  51. // ---------------------------------------------------------------------------------------------------------------------
  52. // Main routine.
  53.  
  54. typedef struct {
  55.     QDGlobals            qd;                                    // Storage for the QuickDraw globals
  56.     long                qdGlobalsPtr;                            // A5 points to this place; it will contain a pointer to qd
  57. } QDStorage;
  58.  
  59. pascal void ShowInitIcon (short iconFamilyID, Boolean advance)
  60. {
  61.     long                oldA5;                                // Original value of register A5
  62.     QDStorage            qds;                                    // Fake QD globals
  63.     CGrafPort            colorPort;
  64.     GrafPort            bwPort;
  65.     Rect                destRect;
  66.     SysEnvRec        environment;                            // Machine configuration.
  67.     
  68.     oldA5 = SetA5((long) &qds.qdGlobalsPtr);                        // Tell A5 to point to the end of the fake QD Globals
  69.     InitGraf(&qds.qd.thePort);                                // Initialize the fake QD Globals
  70.     
  71.     SysEnvirons(curSysEnvVers, &environment);                    // Find out what kind of machine this is
  72.  
  73.     ComputeIconRect(&destRect, &qds.qd.screenBits.bounds);            // Compute where the icon should be drawn
  74.  
  75.     if (environment.systemVersion >= 0x0700 && environment.hasColorQD) {
  76.         OpenCPort(&colorPort);
  77.         PlotIconID(&destRect, atNone, ttNone, iconFamilyID);
  78.         CloseCPort(&colorPort);
  79.     }
  80.     else {
  81.         OpenPort(&bwPort);
  82.         DrawBWIcon(iconFamilyID, &destRect);
  83.         ClosePort(&bwPort);
  84.     }
  85.     
  86.     if (advance)
  87.         AdvanceIconPosition (&destRect);
  88.         
  89.     SetA5(oldA5);                                             // Restore A5 to its previous value
  90. }
  91.  
  92. // ---------------------------------------------------------------------------------------------------------------------
  93. // A checksum is used to make sure that the data in there was left by another ShowINIT-aware INIT.
  94.  
  95. static unsigned short CheckSum (unsigned short x)
  96. {
  97.     return ((x << 1) | (x >> 15)) ^ 0x1021;
  98. }
  99.  
  100. // ---------------------------------------------------------------------------------------------------------------------
  101. // ComputeIconRect computes where the icon should be displayed.
  102.  
  103. static void ComputeIconRect (Rect* iconRect, Rect* screenBounds)
  104. {
  105.     if (CheckSum(LMHCoord) != LMHCheckSum)                    // If we are first, we need to initialize the shared data.
  106.         LMHCoord = 8;
  107.     if (CheckSum(LMVCoord) != LMVCheckSum)
  108.         LMVCoord = screenBounds->bottom - 40;
  109.     
  110.     if (LMHCoord + 34 > screenBounds->right) {                    // Check whether we must wrap
  111.         iconRect->left = 8;
  112.         iconRect->top = LMVCoord - 40;
  113.     }
  114.     else {
  115.         iconRect->left = LMHCoord;
  116.         iconRect->top = LMVCoord;
  117.     }
  118.     iconRect->right = iconRect->left + 32;
  119.     iconRect->bottom = iconRect->top + 32;
  120. }
  121.  
  122. // AdvanceIconPosition updates the shared global variables so that the next extension will draw its icon beside ours.
  123.  
  124. static void AdvanceIconPosition (Rect* iconRect)
  125. {
  126.     LMHCoord = iconRect->left + 40;                            // Update the shared data
  127.     LMVCoord = iconRect->top;
  128.     LMHCheckSum = CheckSum(LMHCoord);
  129.     LMVCheckSum = CheckSum(LMVCoord);
  130. }
  131.  
  132. // DrawBWIcon draws the 'ICN#' member of the icon family. It works under System 6.
  133.  
  134. static void DrawBWIcon (short iconID, Rect *iconRect)
  135. {
  136.     Handle        icon;
  137.     BitMap        source, destination;
  138.     GrafPtr        port;
  139.     
  140.     icon = Get1Resource('ICN#', iconID);
  141.     if (icon != NULL) {
  142.         HLock(icon);
  143.                                                         // Prepare the source and destination bitmaps.
  144.         source.baseAddr = *icon + 128;                        // Mask address.
  145.         source.rowBytes = 4;
  146.         SetRect(&source.bounds, 0, 0, 32, 32);
  147.         GetPort(&port);
  148.         destination = port->portBits;
  149.                                                         // Transfer the mask.
  150.         CopyBits(&source, &destination, &source.bounds, iconRect, srcBic, nil);
  151.                                                         // Then the icon.
  152.         source.baseAddr = *icon;
  153.         CopyBits(&source, &destination, &source.bounds, iconRect, srcOr, nil);
  154.     }
  155. }
  156.  
  157. // ---------------------------------------------------------------------------------------------------------------------
  158. // Notes
  159.  
  160. // Checking for PlotIconID:
  161. // We (PNL) now check for system 7 and colour QD, and use colour graf ports and PlotIconID only if both are true
  162. // Otherwise we use B&W grafport and draw using PlotBWIcon.
  163.